package com.inossem.warehousestorage_inossem.adapter.storage;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.base.StringUtil;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageRespBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageValidBean;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameItemBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.storage.StorageManageMatrRequest;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;


/**
 * Created by cjz on 2017/6/6.
 */

public class StorageManageMatrMoveAdapter extends BaseAdapter {

    private Context mContext;
    private StorageManageMatrBean.DataBean.MaterielListBean bean;
    private List<UpFrameItemBean.DataBean.ListBean> list;
    private LayoutInflater inflater;

    private EditText position,number;

    private Button add;

    private TextView showAndHide;
    private PercentRelativeLayout hideLayout,secondBlock,specialBlock ;

    private TextView
            specialInventoryDescription,
            specialInventoryFlag,
            materielCode,
            specialInventoryCode,
            materielDescription,
            batchCode,
            inventoryNumber,
            sendPosition;

    public StorageManageMatrMoveAdapter(Context context, StorageManageMatrBean.DataBean.MaterielListBean  bean) {
        mContext = context;
        this.bean = bean;
        list = bean.getUpList();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list==null?0:list.size()+1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position-1);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position==0){
            if (convertView == null){
                convertView = inflater.inflate(R.layout.view_storage_manage_matr_move_list_first_item, null);
                this.specialInventoryDescription = (TextView) convertView.findViewById(R.id.specialInventoryDescription);
                this.specialInventoryFlag = (TextView) convertView.findViewById(R.id.specialInventoryFlag);
                this.materielCode = (TextView) convertView.findViewById(R.id.materielCode);
                this.specialInventoryCode = (TextView) convertView.findViewById(R.id.specialInventoryCode);
                this.materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
                batchCode = (TextView) convertView.findViewById(R.id.batchCode);
                inventoryNumber = (TextView) convertView.findViewById(R.id.inventoryNumber);
                sendPosition = (TextView) convertView.findViewById(R.id.sendPosition);
                this.position = (EditText) convertView.findViewById(R.id.position);
                number = (EditText) convertView.findViewById(R.id.number);
                add = (Button)convertView.findViewById(R.id.add);
                hideLayout = (PercentRelativeLayout)convertView.findViewById(R.id.hideLayout);
                secondBlock = (PercentRelativeLayout)convertView.findViewById(R.id.secondBlock);
                specialBlock = (PercentRelativeLayout)convertView.findViewById(R.id.specialBlock);
                showAndHide = (TextView)convertView.findViewById(R.id.showAndHide);

                initFirstItemData();

            }
        }else{
            UpFrameItemBean.DataBean.ListBean item = (UpFrameItemBean.DataBean.ListBean) getItem(position);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_storage_manage_matr_move_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, item);
        }
        return convertView;
    }

    private void initFirstItemData() {
        this.specialInventoryDescription.setText(bean.getSpecialInventoryDescription());
        this.specialInventoryFlag.setText(bean.getSpecialInventoryFlag());
        this.materielCode.setText(bean.getMaterielCode());
        this.specialInventoryCode.setText(bean.getSpecialInventoryCode());
        this.materielDescription.setText(bean.getMaterielDescription());

        batchCode.setText(bean.getBatchCode());
        inventoryNumber.setText(bean.getInventoryNumber());
        sendPosition.setText(bean.getSendPosition());

        //隐藏特殊标识等字段
        if(TextUtils.isEmpty(bean.getSpecialInventoryFlag())){
            specialBlock.setVisibility(View.GONE);
        }

        //展开按钮

        showAndHide.setOnClickListener((View v) -> {
            if (hideLayout.getVisibility() == View.GONE) {
                hideLayout.setVisibility(View.VISIBLE);
                secondBlock.setVisibility(View.VISIBLE);
                ((TextView) v).setText(mContext.getResources().getString(R.string.common_hide_information));
            } else if (hideLayout.getVisibility() == View.VISIBLE) {
                hideLayout.setVisibility(View.GONE);
                secondBlock.setVisibility(View.GONE);
                ((TextView) v).setText(mContext.getResources().getString(R.string.common_show_information));
            }
        });

        add.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                UpFrameItemBean.DataBean.ListBean e = new UpFrameItemBean.DataBean.ListBean();
                if(StringUtil.isNotEmpty(StorageManageMatrMoveAdapter.this.position.getText().toString())&&StringUtil.isNotEmpty(number.getText().toString())){

                    if (!RegexUtils.checkDecimal(number.getText().toString()) || Double.parseDouble(number.getText().toString()) <= 0) {
                        Utils.middleToast(mContext, "本次上架数量不合法");
                        return;
                    }

                    //已经添加的仓位不能再添加
                    boolean isExists = false;
                    String pos = StorageManageMatrMoveAdapter.this.position.getText().toString();
                    for (int i = 0;i<StorageManageMatrMoveAdapter.this.bean.getUpList().size();i++){
                        String pos2 = StorageManageMatrMoveAdapter.this.bean.getUpList().get(i).getPosition();
                        if(pos.equals(pos2)){
                            isExists = true;
                            break;
                        }
                    }
                    if(isExists){
                        Utils.middleToast(mContext,"已经添加的仓位不能再添加");
                        return ;
                    }

                    e.setPosition(StorageManageMatrMoveAdapter.this.position.getText().toString());
                    e.setNumber(number.getText().toString());
                    e.setUnit(bean.getUnit());
                    StorageManageMatrMoveAdapter.this.bean.getUpList().add(0,e);


                    //检查上架的数量
                    if(StorageManageUtils.getUpCount(bean)>Double.parseDouble(bean.getInventoryNumber())){
                        StorageManageMatrMoveAdapter.this.bean.getUpList().remove(e);
                        Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_storage_manage_up_prompt));
                    }else{
                        //远程调用，校验仓位是否可用
                        StorageManageValidBean.DataBean bean = new StorageManageValidBean.DataBean();
                        bean.setLgnum(StorageManageMatrMoveAdapter.this.bean.getLgnum());
                        bean.setReceivePosition(e.getPosition());
                        StorageManageMatrRequest.valid(mContext, bean, new GsonBeanListener<StorageManageRespBean>() {
                            @Override
                            public void onSuccess(StorageManageRespBean bean) {
                                //判断校验
                                if(bean.getData().getCode().equals(RequestConstant.BEAN_CODE_SUCCESS)){

                                    StorageManageMatrMoveAdapter.this.position.setText("");
                                    number.setText("");
                                    StorageManageMatrMoveAdapter.this.notifyDataSetChanged();
                                }else{
                                    StorageManageMatrMoveAdapter.this.bean.getUpList().remove(e);
                                    Utils.middleToast(mContext,mContext.getResources().getString(R.string.activity_storage_manage_novalid_prompt));
                                }

                            }
                        });


                    }


                }



            }
        });

        StorageManageMatrMoveAdapter.this.position.addTextChangedListener(new QRCodeEditTextWatcher(StorageManageMatrMoveAdapter.this.position) {
            @Override
            public void onScan(String text) {
                number.requestFocusFromTouch();
            }
        });
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.postion = (TextView) view.findViewById(R.id.postion);
        viewHolder.number = (TextView) view.findViewById(R.id.number);


    }

    private void initData(Context context, ViewHolder viewHolder, View view, UpFrameItemBean.DataBean.ListBean item) {
        viewHolder.postion.setText(item.getPosition());
        viewHolder.number.setText(item.getNumber()+item.getUnit());

        //事件处理
        view.setOnLongClickListener((View v) -> {
            DialogUtils.getConfirmDialog(mContext, mContext.getResources().getString(R.string.activity_storage_manage_del_prompt) , new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    list.remove(item);
                    StorageManageMatrMoveAdapter.this.notifyDataSetChanged();
                    dialog.cancel();
                }
            }).show();
            return true;
        });

    }

    private class ViewHolder {
        private TextView postion, number;
    }

    public void setList(List<UpFrameItemBean.DataBean.ListBean> list) {
        this.list = list;
    }
}
